/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.database;

import br.com.models.Discipline;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author Valdigleis
 */
public class DaoDisciplineRequisite {
    
    private String sql;
    
    private Connection connection;
    
    /**
     * Método construtor que implementa a classe
     * DaoDisciplineRequisite, que é responsavel pela comunicação
     * do sistema com a base de dados.
     *  
     */
    public DaoDisciplineRequisite(){
        this.connection = Derby.getConnection();
    }
    
    public void saveRequisites(int idDiscipline, int idRequisite){
        try{
            sql = "INSERT INTO SAD.DISCIPLINE_PREREQUISITE" +
                " (discipline, pre)" + " values (?,?)";
            PreparedStatement stmt = connection.prepareStatement(sql);
            stmt.setInt(1, idDiscipline);
            stmt.setInt(2, idRequisite);
            stmt.execute();
            stmt.close();
        }catch(SQLException e){
            System.err.println("Erro ao salvar a pre-requisitos!");
            System.out.println(e.getMessage());
        }
    }
    
    public ArrayList<Integer> getAllPre(int idDiscipline){
        ArrayList<Integer> pre = new ArrayList<Integer>();
        try{
            sql = "SELECT * FROM SAD.DISCIPLINE_PREREQUISITE WHERE discipline=?";
            PreparedStatement stmt = connection.prepareStatement(sql);
            stmt.setInt(1, idDiscipline);
            ResultSet rs = stmt.executeQuery(); 
            while(rs.next()){
                Integer i = new Integer(rs.getInt("pre"));
                pre.add(i);
            }
            return pre;
        }catch(SQLException e){
            System.err.println("Erro ao pegar todos os pre-requisitos!");
            System.out.println(e.getMessage());
        }
        return null;
    }
    
    
    public ArrayList<Integer> getPos(int idDiscipline){
        ArrayList<Integer> pre = new ArrayList<Integer>();
        try{
            sql = "SELECT * FROM SAD.DISCIPLINE_PREREQUISITE WHERE pre=?";
            PreparedStatement stmt = connection.prepareStatement(sql);
            stmt.setInt(1, idDiscipline);
            ResultSet rs = stmt.executeQuery(); 
            while(rs.next()){
                Integer i = new Integer(rs.getInt("pre"));
                pre.add(i);
            }
            return pre;
        }catch(SQLException e){
            System.err.println("Erro ao pegar todos os pre-requisitos!");
            System.out.println(e.getMessage());
        }
        return null;
    }
    
    public void removerPre(int pre){
        try{
            sql = "DELETE FROM SAD.DISCIPLINE_PREREQUISITE WHERE discipline=?";
            PreparedStatement stmt = connection.prepareStatement(sql);
            stmt.setInt(1, pre);
            stmt.execute();
            stmt.close();
        }catch(SQLException e){
            System.err.println("Erro ao pegar todos os pre-requisitos!");
            System.out.println(e.getMessage());
        }
    }
    
    public void remove(Discipline discipline){
        try{
            sql =  "DELETE FROM SAD.DISCIPLINE_PREREQUISITE WHERE discipline=?";
            PreparedStatement stmt = (PreparedStatement) connection.prepareStatement(sql); 
            stmt.setInt(1, discipline.getId());
            stmt.execute();
            stmt.close();
        }catch(SQLException e){
            System.err.println("Erro ao Excluir turma!");
            System.out.println(e.getMessage());
        }
    }
    
    /**
     * Método que finaliza a conexão com a base de dados.
     */
    public void closeConnection(){
        try{
            this.connection.close();
        }catch(SQLException e){
            System.err.println("Erro tentar fechar a conexão da classe Days!");
            System.out.println(e.getMessage());
        }
    }
}
